package com.itcat.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.mysql.pojo.SmsHomeAdvertise;
import com.itcat.service.SmsHomeAdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播广告
 */
@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {
    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;


    /**
     * 分页查询广告
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @GetMapping("/list")
    public CommonResult findList(String endTime,
                                 @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 String name,
                                 Integer type) {
        Page<SmsHomeAdvertise> page = smsHomeAdvertiseService.findList(endTime, pageNum, pageSize, name, type);
        CommonPage<SmsHomeAdvertise> restPage = CommonPage.restPage(page);
        return CommonResult.success(restPage);

    }


    /**
     * 获取广告详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult queryOne(@PathVariable Integer id) {

        SmsHomeAdvertise smsAdvertise = smsHomeAdvertiseService.queryOne(id);
        return CommonResult.success(smsAdvertise);
    }

    /**
     * 添加广告
     *
     * @param smsAdvertise
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody SmsHomeAdvertise smsAdvertise) {
        smsHomeAdvertiseService.create(smsAdvertise);
        return CommonResult.success();
    }

    /**
     * 删除多个广告
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@PathVariable List<Integer> ids) {
        smsHomeAdvertiseService.delete(ids);
        return CommonResult.success();
    }

    /**
     * 根据id删除广告
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult deleteById(@PathVariable Integer id) {
        smsHomeAdvertiseService.deleteById(id);
        return CommonResult.success();
    }

    /**
     * 根据ID修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public CommonResult updateStatus(@PathVariable Integer id, @RequestParam Integer status) {

        smsHomeAdvertiseService.updateStatus(id, status);
        return CommonResult.success();
    }

    /**
     * 根据ID修改广告
     *
     * @param smsHomeAdvertise
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@RequestBody SmsHomeAdvertise smsHomeAdvertise, @PathVariable Integer id) {

        smsHomeAdvertiseService.updateById(id, smsHomeAdvertise);
        return CommonResult.success();
    }

}
